<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        .laytable-cell-customization {
            text-align: center;
            height: auto !important;
            white-space: normal;
            overflow: visible;
            text-overflow: inherit;
            word-break: break-all;
        }
    </style>
</head>
<body>
<!-- 添加筛选功能 -->
<div class="layuimini-container">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">

                        <div class="layui-inline">
                            <label class="layui-form-label">测评师编号</label>
                            <div class="layui-input-inline">
                                <input type="text" name="id" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">社交平台</label>
                            <div class="layui-input-inline">
                                <select name="socialPlatforms" lay-filter="platform">
                                    <option value="">请选择社交平台</option>
                                    <option value="tiktok">抖音</option>
                                    <option value="littleRedBook">小红书</option>
                                    <option value="otherPlatforms">其他社交平台</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">粉丝区间</label>
                            <div class="layui-input-inline fanZone">
                                <select id="zan-unit" lay-filter="zan-unit">
                                    <option value="">请选择</option>
                                    <option value="bai">百赞</option>
                                    <option value="qian">千赞</option>
                                    <option value="wan">万赞</option>
                                </select>
                            </div>
                            <div class="layui-input-inline fanZone">
                                <select id="zan-range" name="fanZone" lay-filter="zan-range">
                                    <option value="">请选择</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">测评师等级</label>
                            <div class="layui-input-inline">
                                <select name="evaluationGrade" lay-filter="platform">
                                    <option value="">请选择测评师等级</option>
                                    <option value="A">A</option>
                                    <option value="B">B</option>
                                    <option value="C">C</option>
                                    <option value="F">F</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">平台ID</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userId" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">关联手机</label>
                            <div class="layui-input-inline">
                                <input type="text" name="phoneNumber" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">性别</label>
                            <div class="layui-input-inline">
                                <select name="sex" lay-filter="sex">
                                    <option value="">不限</option>
                                    <option value="男">男</option>
                                    <option value="女">女</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">年龄层</label>
                            <div class="layui-input-inline">
                                <select name="ageGroup">
                                    <option value="">全部</option>
                                    <option value="10后">10后</option>
                                    <option value="00后">00后</option>
                                    <option value="90后">90后</option>
                                    <option value="80后">80后</option>
                                    <option value="70后">60后</option>
                                    <option value="60后">60后</option>
                                </select>
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">个人身份</label>
                            <div class="layui-input-inline">
                                <select name="identityJob">
                                    <option value="">全部</option>
                                    <option value="在校学生">在校学生</option>
                                    <option value="职场达人">职场达人</option>
                                    <option value="全职宝妈">全职宝妈</option>
                                    <option value="自由职业">自由职业</option>
                                    <option value="网红达人">网红达人</option>
                                    <option value="其他">其他</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">注册时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="dateRange" id="dateRange" class="layui-input"
                                       placeholder="请选择时间区间">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索
                            </button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>
        <table id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="imgTemp">
            <img src="{{d.avatarUrl}}" style="width: 100%;height: 45px;"/>
        </script>
    </div>

    <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
            <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" id="deleteBtn" style="display: none" lay-event="delete">删除 </button>
            <button class="layui-btn layui-btn-warm layui-btn-sm data-add-btn" id="exportBtn" lay-event="export"> 导出</button>
            <button class="layui-btn layui-btn-normal layui-btn-sm" id="ratingBtn" lay-event="rating"> 评级</button>
            <!--            <button class="layui-btn layui-btn-sm layui-btn-disabled data-delete-btn provide-edit"  style="display: none" id="editBtn" lay-event="edit">修改</button>-->
        </div>
    </script>

    <div id="popup" style="display:none">
        <div class="layui-form">
            <div class="layui-form-item">
                <label class="layui-form-label">选择等级：</label>
                <div class="layui-input-block">
                    <select name="level" lay-filter="level">
                        <option value="">请选择等级</option>
                        <option value="A">A</option>
                        <option value="B">B</option>
                        <option value="C">C</option>
                        <option value="F">F</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="edit_level">确定</button>
                </div>
            </div>
        </div>
    </div>

</div>
</body>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../lib/common/common.js" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../lib/common/permissions.js" charset="utf-8"></script>
<script>
    layui.use(['table', 'form', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            laydate = layui.laydate;

        // 执行一个laydate实例
        laydate.render({
            elem: '#dateRange', //指定元素
            max: 0, // 限制最大值为当前日期
            type: 'date',
            range: true, //开启范围选择
        });

        // 表格数据
        const followersData = {
            bai: [
                {id: '1', min: '1', max: '49'},
                {id: '2', min: '50', max: '99'},
                {id: '3', min: '100', max: '499'},
                {id: '4', min: '500', max: '999'}
            ],
            qian: [
                {id: '1', min: '1000', max: '2999'},
                {id: '2', min: '3000', max: '4999'},
                {id: '3', min: '5000', max: '6999'},
                {id: '4', min: '7000', max: '9999'}
            ],
            wan: [
                {id: '1', min: '1w', max: '3w'},
                {id: '2', min: '3w', max: '5w'},
                {id: '3', min: '5w', max: '10w'},
                {id: '4', min: '10w', max: '20w'},
                {id: '5', min: '20w', max: '50w'},
                {id: '6', min: '50w', max: '100w'},
                {id: '7', min: '100w以上', max: ''}
            ]
        };

        // 监听赞数单位选择器
        form.on('select(zan-unit)', function (data) {
            // 清空区间选择器
            $('#zan-range').html('<option value="">请选择</option>');
            // 根据赞数单位渲染区间选择器
            const rangeList = data.value ? followersData[data.value] : [];
            for (var i = 0; i < rangeList.length; i++) {
                var range = rangeList[i];
                var html = '<option value="' + range.min + '-' + range.max + '">' + range.min + '-' + range.max + '</option>';
                if (data.value === 'wan' && range.id === '7') {
                    $('#zan-range').append('<option value="' + "100w以上" + '">' + "100w以上" + '</option>');
                } else {
                    $('#zan-range').append(html);
                }

                form.render('select');
            }
        });

        table.render({
            elem: '#currentTableId',
            url: '/personalInfo/page/list',
            method: 'POST',
            contentType: 'application/json',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: 'checkbox', fixed: 'left', style: 'height:60px;'},
                {
                    field: 'numbering', title: '序号', width: 60, align: 'center', templet: function (d) {
                        return d.LAY_INDEX; //返回序号
                    }
                },
                {
                    field: 'avatarUrl',
                    title: '头像',
                    align: 'center',
                    templet: '#imgTemp',
                    style: 'height:60px;',
                    minWidth: 80
                },
                // 序号、头像、创建时间、测评师编号、测评喜好(主)、测评喜好(副)、兴趣领域、抖音平台（有/无）、粉丝数、获赞数、小红书平台（有/无）、粉丝数、获赞与收藏数、其他社交平台                {field: 'createTime', title: '创建时间', sort: true, align: 'center'},
                {field: 'id', title: '测评师编号', sort: true, align: 'center'},
                {field: 'mainCategory', title: '测评喜好(主)', sort: true, align: 'center'},
                {field: 'secondCategory', title: '测评喜好(副)', sort: true, align: 'center'},
                {field: 'interests', title: '兴趣领域', sort: true, align: 'center'},
                {field: 'tiktok', title: '抖音平台（有/无）', sort: true, align: 'center'},
                {field: 'tiktokFans', title: '粉丝数', sort: true, align: 'center'},
                {field: 'tiktokLikes', title: '获赞数', sort: true, align: 'center'},
                {field: 'littleRedBook', title: '小红书平台（有/无）', sort: true, align: 'center'},
                {field: 'littleRedBookFans', title: '粉丝数', sort: true, align: 'center'},
                {field: 'littleRedBookLikes', title: '获赞与收藏数', sort: true, align: 'center'},
                {field: 'otherPlatforms', title: '其他社交平台', sort: true, align: 'center'},

                // 平台ID、平台昵称、关联手机、真实姓名、性别、年龄层、所在地区、个人身份、月收入、测评师等级
                {field: 'userId', title: '平台ID', sort: true, align: 'center'},
                {field: 'userName', title: '平台昵称', sort: true, align: 'center'},
                {field: 'phoneNumber', title: '关联手机', sort: true, align: 'center'},
                {field: 'realName', title: '真实姓名', sort: true, align: 'center'},
                {field: 'sex', title: '性别', sort: true, align: 'center'},
                {field: 'ageGroup', title: '年龄层', sort: true, align: 'center'},
                {field: 'region', title: '所在地区', sort: true, align: 'center'},
                {field: 'identityJob', title: '个人身份', sort: true, align: 'center'},
                {field: 'monthlyIncome', title: '月收入', sort: true, align: 'center'},
                {field: 'evaluationGrade', title: '测评师等级', sort: true, align: 'center'},
                // {fixed: 'right', title: '操作', toolbar: '#currentTableBar', width: 150}
            ]],
            page: true,
            limits: [50, 100, 150, 200, 250, 300],
            limit: 50,
            request: {
                pageName: 'pageNum',
                limitName: 'pageSize'
            },
            text: {
                none: '暂无数据'
            },
            response: { //定义后端 json 格式，详细参见官方文档
                statusName: 'code', //状态字段名称
                statusCode: '200', //状态字段成功值
                msgName: 'msg', //消息字段
                countName: 'total', //总数字段
                dataName: 'rows' //数据字段
            },
            done: function () {// 表格渲染完成的回调函数
                const btnRoles = {
                    "editBtn": 'admin',
                    "deleteBtn": 'admin',
                }
                buttonPermissions(btnRoles)
                // 给指定的列加上css样式
                let colsList = this.cols[0];
                myClos = colsList;
                for (let cols of colsList) {
                    if (cols.field === 'avatarUrl' || cols.title === '操作') {
                        // 添加样式
                        let classStr = ".laytable-cell-1-" + cols.key;
                        $(classStr).addClass('laytable-cell-customization')
                    }
                }
            }
        });

        // 监听表格排序事件
        table.on('sort(currentTableFilter)', function (obj) {
            for (let cols of myClos) {
                if (cols.field === 'avatarUrl' || cols.title === '操作') {
                    // 添加样式
                    let classStr = ".laytable-cell-1-" + cols.key;
                    $(classStr).addClass('laytable-cell-customization')
                }
            }
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'delete') {  // 监听删除操作
                let checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data.length > 0) {
                    layer.confirm('确定删除吗', function (index) {
                        let idList = []
                        for (const i in data) {
                            idList.push(data[i].id);
                        }
                        $.ajax({
                            type: "post",
                            url: '/personalInfo/delete',
                            data: JSON.stringify(idList),
                            dataType: 'json',
                            contentType: 'application/json',
                            success: function (req) {
                                if (req.code == 200) {
                                    layer.msg("已删除");
                                    reloadTable();
                                } else {
                                    layer.msg("删除失败");
                                }
                            },
                            error: function (e) {
                                layer.msg("删除失败");
                            }
                        });
                    });
                } else {
                    layer.alert('请至少选中一行数据');
                }
            }
            if (obj.event === 'export') {  // 监听导出操作
                let checkStatus = table.checkStatus('currentTableId');
                let data = {
                    ids: [],
                    batchType: 0,
                    searchVo: obj.config?.where,
                }
                if (checkStatus.isAll) {
                    data.batchType = 1;
                } else {
                    let dataList = checkStatus.data;
                    if (dataList.length > 0) {
                        let ids = [];
                        dataList.forEach(function (item) {
                            ids.push(item.id)
                        });
                        data.ids = ids;
                    } else {
                        data.batchType = 1;
                    }

                }
                downloadExcel('/personalInfo/list/export', data, '测评师.xlsx', 'POST')
            }
            if (obj.event === 'rating'){
                let checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                if (data.length > 0) {
                    var popup = $("#popup").html();
                    layer.open({
                        type: 1,
                        title: "修改等级",
                        content: popup,
                        area: ['400px', '300px'],
                        success: function (layero, index) {
                            // 弹出层加载完成后，初始化下拉框的值
                            var level = "A"; // 假设初始等级为A
                            $("select[name='level']").val(level)
                            layui.form.render(); // 渲染表单元素
                        }
                    });
                } else {
                    layer.alert('请至少选中一行数据');
                    return
                }
            }
            return false;
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let dataRangeList = data.field.dateRange.split(` - `)
            let dataRange = {}
            if (dataRangeList.length === 2) {
                dataRange = {
                    'startDate': dataRangeList[0],
                    'endDate': dataRangeList[1]
                }
            }
            data.field.dateRange = JSON.stringify(dataRange);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: data.field
            }, 'data');

            return false;
        });

        // 监听表格双击事件
        table.on('rowDouble(currentTableFilter)', function (obj) {
            const rowData = obj?.data;
            if (rowData !== void 0 && rowData !== null) {
                const index = layer.open({
                    title: '测评师资料详情',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: './personalInfo-edit.html?id=' + rowData.id,
                    end: function () {
                        //关闭弹框后触发
                        reloadTable();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else {
                layer.msg("数据异常，请重新尝试");
            }
            return false;
        });

        // 监听评级提交事件
        layui.form.on('submit(edit_level)', function (data) {
            var dataList = table.checkStatus('currentTableId')?.data;
            let personalInfoIds = [];
            dataList.forEach(function (item) {
                personalInfoIds.push(item.id)
            });
            if (personalInfoIds.length === 0) {
                layer.msg("数据异常，请联系系统管理员");
            }
            let level = data.field?.level; // 获取用户选择的等级
            if (level === void 0 || level === '') {
                layer.msg("数据异常，请联系系统管理员");
            }
            // 构建请求参数
            const requestData = {
                ids: personalInfoIds,
                evaluationGrade: level
            }
            $.ajax({
                url: "/personalInfo/edit/evaluationGrade",
                method: "POST",
                dataType: 'json',
                contentType: 'application/json',
                data: JSON.stringify(requestData),
                success: function (result) {
                    if (result.code === '200') {
                        layer.msg("操作成功")
                        reloadTable();
                    } else {
                        layer.msg(result.msg);
                    }
                    layer.closeAll();
                },
                error: function (xhr, status, error) {
                    // 处理请求失败后的操作
                    layer.msg("请求失败：" + error);
                }
            });

            return false; // 阻止表单默认提交
        });

    })

    /**
     * 重载表格数据
     */
    function reloadTable() {
        layui.use(['table'], function () {
            let table = layui.table;
            table.reload('currentTableId', {
                url: '/personalInfo/page/list'
            });
        })
    }
</script>
<style>
    .fanZone {
        width: 90px !important;
    }

    .fanZone select {
        width: 98px;
    }
</style>
</html>
